package cn.kgc.controller.logic.login;

import cn.kgc.pojo.*;
import cn.kgc.service.administrative.smc_MingPianService;
import cn.kgc.service.hr.QingJiaService;
import cn.kgc.service.login.loginService;
import cn.kgc.service.system.smj_zwqxService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/login")
public class login {

    @Resource
    private  smj_zwqxService smzw;//日志用

    @Resource
    private loginService loginService;

    @Resource
    public smj_zwqxService zhiWeiQuanXian;

    @Resource
    private QingJiaService qingJiaService;

    @Resource
    private smc_MingPianService mpService;

    @RequestMapping("/tologin")
    public String dengLu() {
        return "index";
    }

    @RequestMapping("/selUser")
    public String selUser(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        User user = loginService.selUser(username, password);
        if (user != null) {
            Department s = qingJiaService.selZhiW(user.getJt_id());
            Jobtitle jobtitle = mpService.selJobById(user.getJt_id());
            Zhiweiquanxian zwqx = zhiWeiQuanXian.selJobQXByJbId(user.getJt_id());
            List<Quanxianinfo> quanxianinfos = zhiWeiQuanXian.selQXPathByAuths(splitStr(zwqx.getAuths()));
            session.setAttribute("quanxianinfos", JSON.toJSONString(quanxianinfos));
            session.setAttribute("zhiWei", jobtitle);
            session.setAttribute("user", user);
            session.setAttribute("buMeng", s);
            smzw.insertLog(user.getId(),"登录账户-用户端");//记录日志--佘明杰
            return "index";
        } else {
            model.addAttribute("error", "用户名或密码错误！");
            return "login";
        }
    }

    /**
     *修改密码
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public Boolean updatePwd(Integer id ,String pwd){
        smzw.insertLog(id,"修改了密码");//记录日志--佘明杰
        Integer integer = loginService.updatePwd(id, pwd);
        if (integer>0){
            return true;
        }
        return false;
    }


    /**
     * 分割字符串方法
     */
    public List splitStr(String str) {
        List list = new ArrayList();
        String[] split = str.split("-");

        for (int i = 0; i < split.length; i++) {
            if (split[i] != "0"){
                System.out.println("split[i] = " + split[i]);
                list.add(Integer.valueOf(split[i]));
            }
        }
        if (list.size() == 0) {
            list.add(-1);
        }
        return list;
    }


    /**
     *个人信息
     */
    @RequestMapping("/selUserInfo")
    @ResponseBody
    public User selUserInfo(Integer id ){
        User users = loginService.selUserInfo(id);
        return users;
    }


}
